<?php
/*
	$test=new Transform();
	$xmlUrl = "feed.xml"; // XML feed file/URL
	$xmlStr = file_get_contents($xmlUrl);
	$array=$test->XmlToArray($xmlStr);
	$jsondate=$test->ArrayToJson($array);
	$jsonarr=$test->JsonToArray($jsondate);
	print_r($array);
	print_r($jsondate);
	print_r($jsonarr);
 *
 *	Transform.php
 *	public 
 *	@param GetRequest
 *			@@param array $param
 *			@@param boolen $withget
 *			@@return array
 *	@param GetQuery
 *			@@param array $request
 *			@@return string
 *	@param JsonToArray
 *			@@param string $jsonstring
 *			@@return array
 *	@param ArrayToJson
 *			@@param array $arrdata
 *			@@return json
 *	@param XmlToArray
 *			@@param string $xmlstring
 *			@@return array
 *	@param ArrayToXml
 *			@@param	array $data
 *			@@param	string $rootNodeName = 'data'
 *			@@param	XML document $xml=NULL
 *			@@return XML string
 *
 *	private
 *	@param objectsIntoArray
 *			@@param	XML document/array $arrObjData
 *			@@return array
 */
require_once LIB_PATH.'Unserializer.php';
class Transform{
	/**
	 * Remove the invalid value in array.
	 * @param array $param
	 * @param boolen $withget
	 * @return array
	 */
	public function GetRequest($param=array(),$withget=true){
		$newparam=array();
		if(is_array($param)){
			foreach($param as $key=>$val){
				if(isset($val)&&strlen($val))$newparam[$key]=$val;
				$newparamval = _GET($key);
				if($newparamval!=NULL&&$withget)$newparam[$key]=$newparamval;
				//if(!empty($_GET[$key])&&$withget)$newparam[$key]=$_GET[$key];
			}
		}
		ksort($newparam);
		return $newparam;
	}
	/**
	 * Array to querystring.
	 * @param array $request
	 * @return string
	 */
	public function GetQuery($request=array()){
		$newparam = array();
		if(is_array($request)){
			foreach ($request as $param=>$value)
			{
				if($param!="Signature"){
					$param = str_replace("%7E", "~", rawurlencode($param));
					$value = str_replace("%7E", "~", rawurlencode($value));
				}
				$newparam[] = $param."=".$value;
			}
		}
		return implode("&", $newparam);
	}
	/**
	 * Json to array.
	 * @param string $jsonstring
	 * @return array
	 */
	public function JsonToArray($jsonstring){
		if(!function_exists("json_decode")){
			require_once("../lib/JSON.php");
			$json = new Services_JSON;
			$objData = $json->decode($jsonstring);
		}else{$objData = json_decode($jsonstring);}
		return $this->objectsIntoArray($objData);
	}
	/**
	 * Array to json.
	 * @param array $arrdata - array data
	 * @return string json
	 */
	public function ArrayToJson($arrdata){
		if(!function_exists("json_encode")){
			require_once("../lib/JSON.php");
			$json = new Services_JSON;
			$result = $json->encode($arrdata);
		}else{$result = json_encode($arrdata);}
		return $result;
	}
	/**
	 * XML document To array.
	 * @param string $xmlstring - xmldata
	 * @return array
	 */
	public function XmlToArray($xmlstring){
		$test = $this->getXml($xmlstring);
		//print_r($test);
		return $test;
		//$xmlstring = preg_replace("/\&.[^\W]*;/is", "",$xmlstring);
		$xmlObj = simplexml_load_string($xmlstring);
		print_r($this->objectsIntoArray($xmlObj));
		return $this->objectsIntoArray($xmlObj);
	}
    public function getXml($xmlstring){
		// ＸＭＬをパースして返却
		$unserializer = new XML_Unserializer();
		$unserializer->setOption( "parseAttributes", true );
		$ans = $unserializer->unserialize( $xmlstring );
		if( PEAR::isError( $ans ) ){return false;} 
		// 返却
		return $unserializer->getUnserializedData();
    	
    }
	/**
	 * Array to an XML document.
	 * @param array $data - array.
	 * @param string $rootNodeName - root node.
	 * @param SimpleXMLElement $xml - XML document.
	 * @return string XML
	 */
	public function ArrayToXml($data, $rootNodeName = 'data', $xml=NULL){
		if ($xml == null){
			$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
		}
		// loop through the data passed in.
		foreach($data as $key => $value)
		{
			// no numeric keys in our xml please!
			if (is_numeric($key))
			{
				// make string key...
				$key = "unknownNode_". (string) $key;
			}
 
			// replace anything not alpha numeric
			$key = preg_replace('/[^a-z]/i', '', $key);
 
			// if there is another array found recrusively call this function
			if (is_array($value))
			{
				$node = $xml->addChild($key);
				// recrusive call.
				$this->ArrayToXml($value, $rootNodeName, $node);
			}
			else 
			{
				// add single node.
				//$value = htmlentities($value);
				$value = htmlspecialchars($value);
				$xml->addChild($key,$value);
			}
 
		}
		// pass back as string. or simple xml object if you want!
		return $xml->asXML();
	}
	/**
	 * XML document or object To array .
	 * @param string $xmlstring - xmldata
	 * @return array
	 */
	private function objectsIntoArray($arrObjData)
	{
		$arrData = array();
		// if input is object, convert into array
		if (is_object($arrObjData)) {
			$arrObjData = get_object_vars($arrObjData);
		}
		if (is_array($arrObjData)) {
			foreach ($arrObjData as $index => $value) {
				if (is_object($value) || is_array($value)) {
					$value = $this->objectsIntoArray($value); // recursive call
				}
				$arrData[$index] = $value;
			}
		}
		return $arrData;
	}
}
?>